package com.iot08.api.service;

import com.iot08.common.entity.app.UserKeyAction;
import com.iot08.common.service.base.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 用户关键行为
 */
@Slf4j
@Service
public class UserKeyActionService extends BaseService<UserKeyAction> {


    /**
     * 得到request对象
     */
    protected HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    /**
     * 获取当前请求ip地址
     *
     * @time: 2020/9/10 9:38
     * @author: Rock
     * @version 1.0
     */
    protected String getRemortIP() {
        HttpServletRequest request = getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (StringUtils.isEmpty(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 添加用户关键行为
     * 行为类型
     * 1:注册
     * 2:注销账号
     * 3:登录/退出登录
     * 4:修改密码
     * 5:修改手机号
     * 6:绑定设备
     * 7:解绑设备
     * 8:恢复出厂设置
     * 9:修改设备资料
     * 10:敏感词违规
     * @param actiontype 关键类型
     * @param openid openid
     * @param des 描述
     */
    public void add(Integer actiontype, String openid, String des) {

        String phoneType = "";
        String ip = "";

        try {
            UserKeyAction userKeyAction = new UserKeyAction();
            HttpServletRequest request = getRequest();
            if(request != null){
                String userAgent = request.getHeader("User-Agent");
                if (!StringUtils.isEmpty(userAgent)) {
                    String[] sysInfoList = userAgent.split(";");
                    String[] tempArr = sysInfoList[0].split("\\(");
                    phoneType = tempArr[tempArr.length - 1];
                }
                ip = getRemortIP();
                userKeyAction.setIp(ip);
                userKeyAction.setPhoneType(phoneType);
                String mac = request.getHeader("mac");
                String imei = request.getHeader("imei");
                String androidId = request.getHeader("androidId");
                String OAID = request.getHeader("OAID");
                String IMSI = request.getHeader("IMSI");

                if(!StringUtils.isEmpty(mac)){
                    userKeyAction.setMac(mac);
                }
                if(!StringUtils.isEmpty(imei)){
                    userKeyAction.setImei(imei);
                }
                if(!StringUtils.isEmpty(androidId)){
                    userKeyAction.setAndroidId(androidId);
                }
                if(!StringUtils.isEmpty(OAID)){
                    userKeyAction.setOAID(OAID);
                }
                if(!StringUtils.isEmpty(IMSI)){
                    userKeyAction.setIMSI(IMSI);
                }
            }

            userKeyAction.setOpenid(openid);
            userKeyAction.setActiontype(actiontype);
            userKeyAction.setDes(des);
            userKeyAction.setCreatetime(new Date());

            this.save(userKeyAction);

        }catch (Exception e){
            log.error("获取手机型号 或 IP有问题:actiontype:{}, openid:{}, des:{}", actiontype, openid, des);
            log.error("获取手机型号 或 IP有问题: error:"+e.getMessage(), e);
        }
    }


    public static void main(String[] args) {

    }
}
